﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Scheduling;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;

    public class CleanUpCarts : SchedulerClient
    {
        public CleanUpCarts(ScheduleHistoryItem objScheduleHistoryItem)
        {
            this.ScheduleHistoryItem = objScheduleHistoryItem;
        }

        private void DoCleanUpCarts()
        {
            IEnumerator enumerator = null;
            CartController controller = new CartController();
            int orderMins = 0xb40;
            int cartMins = 0x2760;
            SettingsController controller3 = new SettingsController();
            PortalController controller2 = new PortalController();
            ArrayList allPortals = controller3.GetAllPortals();
            try
            {
                enumerator = allPortals.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    PortalInfo current = (PortalInfo) enumerator.Current;
                    NB_Store_SettingsInfo info2 = controller3.GetSetting(current.PortalID, "purgecartmins", SharedFunctions.GetCurrentCulture());
                    if (info2 != null)
                    {
                        cartMins = Conversions.ToInteger(info2.SettingValue);
                    }
                    info2 = controller3.GetSetting(current.PortalID, "purgeordermins", SharedFunctions.GetCurrentCulture());
                    if (info2 != null)
                    {
                        orderMins = Conversions.ToInteger(info2.SettingValue);
                    }
                    controller.DeleteOldCarts(current.PortalID, cartMins, orderMins);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public override void DoWork()
        {
            try
            {
                this.Progressing();
                this.DoCleanUpCarts();
                this.ScheduleHistoryItem.Succeeded = true;
                this.ScheduleHistoryItem.AddLogNote("Store Clean Up Carts Completed.");
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                this.ScheduleHistoryItem.Succeeded = false;
                this.ScheduleHistoryItem.AddLogNote("Store Clean Up Carts failed." + objException.ToString());
                this.ScheduleHistoryItem.Succeeded = false;
                this.Errored(ref objException);
                DotNetNuke.Services.Exceptions.Exceptions.LogException(objException);
                ProjectData.ClearProjectError();
            }
        }
    }
}

